package com.train.view;

import java.awt.Color;
import java.awt.Font;
import java.awt.Toolkit;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.MouseAdapter;
import java.awt.event.MouseEvent;
import java.sql.SQLException;

import javax.swing.DefaultComboBoxModel;
import javax.swing.ImageIcon;
import javax.swing.JButton;
import javax.swing.JCheckBox;
import javax.swing.JComboBox;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JOptionPane;
import javax.swing.JPanel;
import javax.swing.JPasswordField;
import javax.swing.JTextField;
import javax.swing.border.EmptyBorder;

import com.dao.UserDAO;
import com.daofactory.DAOFactory;
import com.train.util.WindowUtil;
import com.train.vo.User;

@SuppressWarnings("serial")
public class LoginForm extends JFrame {
	private JPanel contentPane;
	private JTextField username;
	private JPasswordField password;
	private JPanel facePane;
	private JLabel L_face;
	private JPanel infoPane;
	private JButton btnLogin;
	private JLabel L_title;

	public static void main(String[] args) {
		new LoginForm().setVisible(true);
	}
	
	private LoginForm() {
		setTitle("LoginForm");
		setIconImage(Toolkit.getDefaultToolkit().getImage("./images/face.jpg"));
		setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
		setBounds(100, 100, 417, 299);

		init();
		setLocation(WindowUtil.setLocation(this.getWidth(), this.getHeight()));
	}

	public static void display() {
		new LoginForm().setVisible(true);
	}

	private void init() {
		contentPane = new JPanel();
		contentPane.setBackground(Color.WHITE);
		contentPane.setBorder(new EmptyBorder(5, 5, 5, 5));
		setContentPane(contentPane);
		contentPane.setLayout(null);

		facePane = new JPanel();
		facePane.setBackground(Color.WHITE);
		facePane.setBounds(43, 75, 98, 95);
		contentPane.add(facePane);
		facePane.setLayout(null);

		L_face = new JLabel("");
		L_face.setBackground(Color.WHITE);
		L_face.setIcon(new ImageIcon("./images/xiaogou.png"));
		L_face.setFont(new Font("Segoe Print", Font.BOLD, 16));
		L_face.setBounds(0, 0, 98, 95);
		facePane.add(L_face);

		infoPane = new JPanel();
		infoPane.setBackground(Color.WHITE);
		infoPane.setBounds(151, 75, 232, 95);
		contentPane.add(infoPane);
		infoPane.setLayout(null);

		username = new JTextField();
		username.addMouseListener(new MouseAdapter() {
			@Override
			public void mouseClicked(MouseEvent e) {
				do_username_action(e);
			}

			@Override
			public void mouseExited(MouseEvent e) {
				do_usernameExit_action(e);
			}
		});

		username.setBackground(Color.WHITE);
		username.setFont(new Font("华文行楷", Font.BOLD, 16));
		username.setBounds(10, 10, 212, 33);
		infoPane.add(username);
		username.setColumns(10);

		password = new JPasswordField();
		password.setEchoChar('●');
		password.setFont(new Font("华文行楷", Font.BOLD, 12));
		password.setBounds(10, 53, 212, 33);
		infoPane.add(password);

		btnLogin = new JButton("Login");
		btnLogin.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent e) {
				do_login_action(e);
			}
		});
		btnLogin.setFont(new Font("Segoe Print", Font.BOLD, 18));
		btnLogin.setBounds(161, 186, 209, 39);
		contentPane.add(btnLogin);

		L_title = new JLabel("电厂职工培训管理系统");
		L_title.setFont(new Font("华文行楷", Font.BOLD, 22));
		L_title.setBackground(Color.PINK);
		L_title.setBounds(87, 10, 232, 55);
		contentPane.add(L_title);
	}

	protected void do_usernameExit_action(MouseEvent e) {
		if("".equals(username.getText())) {
			username.setText("用户名");
		}
	}

	protected void do_username_action(MouseEvent e) {
		if ("用户名".equals(username.getText())) {
			username.setText("");
		} else {
			return;
		}
	}

	protected void do_login_action(ActionEvent e) {
		// validate();
		String user_name = this.username.getText().trim();
		char[] pwds = this.password.getPassword();
		String pwd = new String(pwds);
		
		if("".equals(user_name) || "".equals(pwd)) {
			JOptionPane.showMessageDialog(null, "用户名或密码不能为空！");
		}
		
		boolean flag = check(user_name, pwd);

		if (flag) {
			LoginForm.this.setVisible(false);
			TrainMainFrame.display();
		}
	}

	/* 杈撳叆闈炴硶妫�娴� */
	// public void validate() {
	// String user_name = username.getText().trim();
	// char[] codes = password.getPassword();
	// String code = new String(codes);
	//
	// UserDAOImpl dao = new UserDAOImpl();
	// try {
	// User user = dao.findUser(user_name, code);
	//
	// System.out.println(user);
	// if(user != null) {
	// LoginForm.this.setVisible(false);
	// TrainMainFrame.display();
	// }
	// } catch (SQLException e) {
	// e.printStackTrace();
	// }
	// }
	public boolean check(String user_name, String pwd) {
		UserDAO dao = DAOFactory.getUserDAOInstance();
		try {
			User user = dao.findUser(user_name, pwd);

			if (user != null) {
				return true;
			}
		} catch (SQLException e) {
			e.printStackTrace();
		}
		return false;
	}
}
